package cn.yhbk.web.modules.manager.Controller;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Gpa.GpaDto;
import cn.yhbk.web.modules.manager.Pojo.DTO.ScholarShip.ScholarShipDto;
import cn.yhbk.web.modules.manager.Pojo.DTO.ScholarShip.ScholarShipLimit;
import cn.yhbk.web.modules.manager.Pojo.DTO.ScholarShip.ScholarShipTree;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbScholarship;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbStudent;
import cn.yhbk.web.modules.manager.Pojo.VO.Scholarship.ScholarShipPag;
import cn.yhbk.web.modules.manager.Pojo.VO.Scholarship.ScholarShipPeople;
import cn.yhbk.web.modules.manager.Pojo.VO.Scholarship.ScholarshipAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Scholarship.ScholarshipUpdate;
import cn.yhbk.web.modules.manager.Service.ITbGpaRecordService;
import cn.yhbk.web.modules.manager.Service.ITbScholarshipService;
import cn.yhbk.web.modules.manager.Service.ITbStudentService;
import io.minio.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 奖学金信息表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "奖学金信息接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbScholarship")
public class TbScholarshipController {

    @Autowired
    private ITbScholarshipService iTbScholarshipService;

    @Autowired
    private ITbStudentService studentService;

    @Autowired
    private ITbGpaRecordService recordService;

    @ApiOperation("根据ID获取奖学金信息")
    @GetMapping("/getScholarship")
    public CommonResult<TbScholarship> getScholarship(@RequestParam("id") Long id){
        log.info("查询奖学金信息，id：" + id);
        return CommonResult.success(iTbScholarshipService.getById(id));
    }

    @ApiOperation("保存奖学金信息")
    @PostMapping("/saveScholarship")
    public CommonResult<String> saveScholarship(@RequestBody ScholarshipAdd scholarshipAdd){
        Long adminId = MyUtil.getCurrentAdminId();
        Boolean saveScholarship = iTbScholarshipService.saveScholarShip(adminId, scholarshipAdd);
        if(!saveScholarship){
            return CommonResult.failed("保存奖学金信息出错");
        }
        return CommonResult.success("保存奖学金信息成功");
    }

    @ApiOperation("修改某个年级专业奖学金获奖人数限制")
    @PostMapping("/updatePeople")
    public CommonResult updatePeople(@RequestBody ScholarShipPeople scholarShipPeople){
        iTbScholarshipService.updatePeople(scholarShipPeople);

        return CommonResult.success("修改成功");
    }

    @ApiOperation("修改奖学金信息")
    @PutMapping("/updateScholarship")
    public CommonResult<String> updateScholarship(@RequestBody ScholarshipUpdate scholarshipUpdate){
        log.info("修改奖学金信息，" + scholarshipUpdate);
        TbScholarship tbScholarship = iTbScholarshipService.getById(scholarshipUpdate.getId());
        BeanUtils.copyProperties(scholarshipUpdate, tbScholarship);
        tbScholarship.setUpdateTime(LocalDateTime.now());
        Boolean updateScholarship = iTbScholarshipService.updateById(tbScholarship);
        if(!updateScholarship){
            return CommonResult.failed("保存奖学金信息出错");
        }
        return CommonResult.success("保存奖学金信息成功");
    }

    @ApiOperation("根据ID删除奖学金信息")
    @DeleteMapping("/deleteScholarship/{scholarshipId}")
    public CommonResult<String> deleteScholarship(@PathVariable("scholarshipId") Long scholarshipId){
        Boolean deleteScholarship = iTbScholarshipService.removeById(scholarshipId);
        if(!deleteScholarship){
            return CommonResult.failed("删除奖学金信息出错");
        }
        return CommonResult.success("删除奖学金信息成功");
    }

    @ApiOperation("分页获取奖学金信息")
    @GetMapping("/getScholarShipPag")
    public CommonResult<ScholarShipPag> getScholarShipPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                          @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                                                          @RequestParam(name = "typeId", required = false) Long typeId,
                                                          @RequestParam(name = "scholarShipName", required = false) String scholarShipName){
        ScholarShipPag scholarShipPag = iTbScholarshipService.getScholarShipPag(currentPage, pageSize, typeId, scholarShipName);
        return CommonResult.success(scholarShipPag);
    }

    @ApiOperation("获取所有奖学金信息【树形】")
    @GetMapping("/getAllScholarShipMap")
    public CommonResult<List<ScholarShipTree>> getAllScholarShipMap(){
        List<ScholarShipTree> scholarShipTrees = iTbScholarshipService.getAllScholarShip();
        return CommonResult.success(scholarShipTrees);
    }

    @ApiOperation("获取所有奖学金信息【列表】")
    @GetMapping("/getAllScholarShipList")
    public CommonResult<List<ScholarShipDto>> getAllScholarShipList(){
        List<ScholarShipDto> scholarShipTrees = iTbScholarshipService.getAllScholarShipList();
        return CommonResult.success(scholarShipTrees);
    }

    @ApiOperation("学生获取奖学金排序记录【有绩点信息，有奖学金信息，需要奖学金ID，用于获取该奖学金的学生排序】")
    @GetMapping("/getScholarshipSort")
    public CommonResult<List<GpaDto>> getScholarshipSort(@RequestParam("scholarshipId") Long scholarshipId){
        Long adminId = MyUtil.getCurrentAdminId();
        TbStudent student = studentService.getStudentByAdminId(adminId);
        List<GpaDto> gpaDtoList = recordService.getGpaRecordSort(student.getId(), scholarshipId);

        return CommonResult.success(gpaDtoList);
    }

//    @ApiOperation("获取某个奖学金的获奖人数限制情况")
//    @GetMapping("/getLimitNum")
//    public CommonResult<List<ScholarShipLimit>> getLimitNum(@RequestParam("scholarshipId") Long scholarshipId){
//        List<ScholarShipLimit> scholarShipLimitList = iTbScholarshipService.getLimitNum(scholarshipId);
//
//        return CommonResult.success(scholarShipLimitList);
//    }

    @ApiOperation("获取某个专业的获奖人数限制情况")
    @GetMapping("/getLimitNum")
    public CommonResult<List<ScholarShipLimit>> getLimitNum(@RequestParam("majorId") Long majorId, @RequestParam("gradeId") Long gradeId){
        List<ScholarShipLimit> scholarShipLimitList = iTbScholarshipService.getLimitNum(majorId, gradeId);

        return CommonResult.success(scholarShipLimitList);
    }
}
